﻿/*
 *author:keily ,department 2,wuda geo
 *create date:2013-1-28
 *description:集合接口，集合操作类
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace GeoWeb.Common
{
    
    /// <summary>
    /// 集合set的操作类，实现IGeoSet<T>接口
    /// </summary>
    public class GeoSet<T>
    {
        /// <summary>
        /// 获取2个集合的并集
        /// </summary>
        /// <param name="first">集合A</param>
        /// <param name="second">集合B</param>
        /// <returns></returns>
        public static IGeoSet<T> Union(IGeoSet<T> first, IGeoSet<T> second)
        {
            IGeoSet<T> union = new DictionarySet<T>();

            // 迭代第一个集合
            IEnumerator<T> items = first.GetEnumerator();
            while (items.MoveNext())
            {
                if (!union.Contains(items.Current))
                    union.Add(items.Current);
            }
            //迭代第二个集合
            items = second.GetEnumerator();
            while (items.MoveNext())
            {
                if (!union.Contains(items.Current))
                    union.Add(items.Current);
            }
            return union;
        }


        /// <summary>
        /// 获取交集
        /// </summary>
        /// <param name="first">集合A</param>
        /// <param name="second">集合B</param>
        /// <returns></returns>
        public static IGeoSet<T> Intersect(IGeoSet<T> first, IGeoSet<T> second)
        {
            IGeoSet<T> intersect = new DictionarySet<T>();

            //选择比较大的集合来迭代，选择小的集合来查找，优化执行效率
            IGeoSet<T> setToIterate = first;
            IGeoSet<T> setToCheck = second;
            if (second.Count > first.Count)
            {
                setToIterate = second;
                setToCheck = first;
            }

            //迭代集合，查询到新的集合
            IEnumerator<T> items = setToIterate.GetEnumerator();
            while (items.MoveNext())
            {
                if (setToCheck.Contains(items.Current))
                    intersect.Add(items.Current);
            }
            return intersect;
        }


        /// <summary>
        /// 获取一个集合，此集合不包含集合A、集合B的并集
        /// </summary>
        /// <param name="first">集合A</param>
        /// <param name="second">集合B</param>
        /// <returns></returns>
        public static IGeoSet<T> ExclusiveOr(IGeoSet<T> first, IGeoSet<T> second)
        {
            IGeoSet<T> union = new DictionarySet<T>();

            // 迭代集合A
            IEnumerator<T> items = first.GetEnumerator();
            while (items.MoveNext())
            {
                //如果在集合B中不包含，则添加到输出集合中
                if (!second.Contains(items.Current))
                    union.Add(items.Current);
            }
            //迭代集合A
            items = second.GetEnumerator();
            while (items.MoveNext())
            {
                //如果集合A中不包含，则添加到输出集合中
                if (!first.Contains(items.Current))
                    union.Add(items.Current);
            }
            return union;
        }


        /// <summary>
        /// 获取集合A与集合B交集的补集
        /// </summary>
        /// <param name="first">集合A</param>
        /// <param name="second">集合B</param>
        /// <returns></returns>
        public static IGeoSet<T> Minus(IGeoSet<T> first, IGeoSet<T> second)
        {
            IGeoSet<T> minus = new DictionarySet<T>();

            // 迭代集合A
            IEnumerator<T> items = first.GetEnumerator();
            while (items.MoveNext())
            {
                //如果集合A的元素不在集合B中，添加至输出集合中
                if (!second.Contains(items.Current))
                    minus.Add(items.Current);
            }
            return minus;
        }
    }
}
